<?php
/**
 * 车辆管理模块 添加修改
 */

namespace Admin\Controller;

class CarController extends ComController {
    /**
     * 车辆列表
     */
    public function index(){
        $d_id = session('uid'); //驾校id
        $p = I('get.p');
        //分页
        $p = intval($p) > 0 ? $p : 1;
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $car = D('car');
        $where = ' c.d_id = '.$d_id;
        $count_where = ' d_id = '.$d_id;
        $car_num = I('car_num') ? I('car_num') : '';
        $driver_type = I('l_type') ? I('l_type') : 0 ;
        if(!empty($car_num)){
            $where .= ' and c.car_num like "%'.trim($car_num).'%"';
            $count_where .= ' and car_num like "%'.trim($car_num).'%"';
        }
        if($driver_type){
            $where .= ' and c.l_type = '.$driver_type;
            $count_where .= ' and l_type = '.$driver_type;
        }
        $count = $car->where($count_where)->count();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $car_data = $car->alias(' as c')->field(' c.id,c.car_type,c.car_num,c.status,c.d_id,m.name as sc_name,l.type')->join('right join driving_license_type as l  on c.l_type = l.id')->join(' right join driving_member as m on l.d_id = m.uid')->where($where)->limit($offset . ',' . $pagesize)->select();
        // echo $car->getLastSql();die;
        $this->assign('page',$page);
        $this->assign('car',$car_data);
        $license_type = D('license_type');
        $l_type = $license_type->where(array('d_id'=>$d_id))->select();
        $this->assign('license_type',$l_type);
        $this->display();
    }

    /**
     * 车俩记录添加
     */
    
    public function append(){
        $d_id = session('uid');//驾校id
        if(IS_POST){
            $car_type = I('post.car_type') ? I('post.car_type') : '';
            $car_num = I('post.car_num') ? I('post.car_num') : '';
            $l_type = I('post.l_type') ? I('post.l_type') : '';
            if(!$car_type) $this->error('请填写车辆型号');
            if(!$car_num) $this->error('请填写车牌号');
            if(!$l_type) $this->error('请选择所属驾照类型');
            $param['car_type'] = $car_type; 
            $param['car_num'] = $car_num; 
            $param['l_type'] = $l_type;
            $param['d_id'] = $d_id;
            $car = D('car'); //实例化汽车模型
            $res = $car->add($param);
            if($res){
                $this->success('添加车辆信息成功！',U('index'));
            }else{
                $this->error('添加车辆信息失败!');
            }
        }else{
            $license_type = M('license_type');
            $l_data = $license_type->where('d_id = '.$d_id)->select();
            $this->assign('license_type',$l_data);
            $this->display();
        }
    }

    /**
     * 车辆记录修改
     */
    public function edit(){
        $d_id = session('uid');//驾校id
        $id = I('get.id') ? I('get.id') : 0;//修改的主键id
        if(IS_POST){
            $car = D('car');
            $param = I('post.');
            $id = $param['id'];
            unset($param['id']);
            $car_data = $car->where('id = '.$id)->find();
            if($car_data){
                $param['d_id'] = $d_id; //添加驾校id
                $car->where(array('id'=>$id))->save($param);
                $car_num = $param['car_num'];
                //更改教练表中信息
                $coach = M('coach');
                $res = $coach->where(array('car_id'=>$id,'d_id'=>$d_id))->save(array('car_num'=>$car_num));
                    $this->success('修改车辆信息成功！',U('index'));die;
            }else{
               $this->error('修改车辆信息失败,参数错误！');
            }
        }else{
            //获取驾照类型
            $license_type = M('license_type');
            $l_data = $license_type->where('d_id = '.$d_id)->select();
            $this->assign('license_type',$l_data);
            //获取车辆信息
            $car = D('car');
            $car_data = $car->where('d_id = '.$d_id.' and id = '.$id)->find();
            $this->assign('car',$car_data);
            $this->display();
        }
    }

    /**
     * 删除车辆信息
     */
    public function del(){
        $id = I('get.id') ? I('get.id') : 0;
        $d_id = session('uid'); //驾校id
        $where['id'] = $id;
        $where['d_id'] = $d_id;
        $car = D('car');
        $car_data = $car->where($where)->find();
        if($car_data){
            //删除
            $status = $car_data['status'];
            if($status == 0 ){

                $res = $car->where($where)->delete();
                if($res){
                    $this->success('删除车辆信息，成功!',U('index'));
                }else{
                    $this->error('删除车辆信息，失败!'.$car->getError());
                }
            }else{
                $this->error('车辆正被占用不能删除！');
            }
        }else{
            $this->error('没有找到记录，信息删除失败!');
        }


    }


    /**
     * ajax获取所属车型的车辆信息(添加教练信息选择驾照类型)
     */
    public function getCar(){
        $d_id = session('uid');//获取当前驾校id
        $l_type = I('post.l_type') ? I('post.l_type') : 0;
        if($d_id && $l_type){
            $car = D('car');//实例化汽车模型
            $where['l_type'] = $l_type;
            $where['d_id'] = $d_id;
            $where['status'] = 0;
            $data = $car->where($where)->select();
            if($data){
               echo   json_encode(array('res'=>1,'data'=>$data,'msg'=>'成功'));die; 
            }else{
               echo   json_encode(array('res'=>0,'data'=>'','msg'=>'失败'));die; 
            }
        }else{
            echo json_encode(array('res'=>0,'data'=>'','msg'=>'请选择驾照类别！'));die;
        }
    }
}
?>